package com.qzsoft.system.common;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @author gbx
 */

public class PinYinUtils {

    /**
     * 获取汉字简拼
     * @param hanZi 汉字内容
     * @return 简拼内容
     */
    public static String getSpell(String hanZi) {
        HanyuPinyinOutputFormat hyPinYinFormat = new HanyuPinyinOutputFormat();
        hyPinYinFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        hyPinYinFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        hyPinYinFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        StringBuilder simplePinYin = new StringBuilder();
        int iLen = hanZi.length();

        for(int i = 0; i < iLen; ++i) {
            String[] temp = new String[0];

            try {
                char ch = getHanZiChar(hanZi.charAt(i));
                if (ch == 0) {
                    continue;
                }

                temp = PinyinHelper.toHanyuPinyinStringArray(ch, hyPinYinFormat);
                if (temp.length < 1) {
                    temp = new String[]{String.valueOf(ch)};
                }
            } catch (BadHanyuPinyinOutputFormatCombination var7) {
                var7.printStackTrace();
            }

            simplePinYin.append(temp[0].charAt(0));
        }

        return simplePinYin.toString().toUpperCase();
    }

    private static char getHanZiChar(char ch) {
        switch(ch) {
            case '(':
            case ')':
            case '+':
            case ',':
            case '.':
            case '、':
            case '（':
            case '）':
            case '，':
                return '\u0000';
            case 'Ⅰ':
                return 'y';
            case 'Ⅱ':
                return 'e';
            case 'Ⅲ':
                return 's';
            case '仇':
                return 'q';
            case '科':
                return 'k';
            case '调':
                return 't';
            case '长':
                return 'z';
            case '隗':
                return 'w';
            default:
                return ch;
        }
    }

    /**
     * 获取汉字全拼
     * @param hanZi 汉字内容
     * @return 全拼内容
     */
    public static String getFullPinYin(String hanZi) {
        StringBuffer pybf = new StringBuffer();
        char[] arr = hanZi.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 128) {
                try {
                    pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pybf.append(arr[i]);
            }
        }
        return pybf.toString();
    }
}
